<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;

use Encore\Admin\Grid;
use Encore\Admin\Form;
use App\Models\NoticePlat;

use function foo\func;
use Illuminate\Http\Request;
use App\Admin\Extra\Search\Html;
use \Illuminate\Support\Facades\Input;

use App\Models\Area;
use function PHPSTORM_META\elementType;
use Redirect;
class NoticePlatController extends Controller
{
    public function index()
    {
        return Admin::content(function (Content $content)use($use_data) {
            $content->header('mc list');
            $content->row(Html::append([
                Html::likeRedirect('title', 'notice title', url()->current()),
                Html::button('GO', url()->current(), 'search-filter'),
                Html::sjs(url()->current()),
            ]));	

			$grid = Admin::grid(NoticePlat::class, function(Grid $grid) use($use_data){

                $grid->actions(function($actions)use(&$row){
                    $row = $actions->row;
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $actions->append(Html::button('EDIT', '/admin/notice-plat/edit?mc='.$actions->row->mc.'&lang='.$actions->row->lang, 'btn-goto btn-xs'));
                    //$actions->append(Html::button('DEL', '/admin/notice-plat/api-save?mc='.$actions->row->mc.'&deleted_at=1', 'btn-goto btn-danger btn-xs btn-clear'));
                });

			    // 第一列显示id字段，并将这一列设置为可排序列
			    $grid->id('ID')->sortable();
                $grid->name('name');
                $grid->mc('mc');
                $grid->lang('lang');
                $grid->lang_alias('lang_alias');
            });
        
            $content->row($grid->render());

        });
    }

    public function anyEdit(){
        $mc = Input::get('mc');
        $lang = Input::get('lang');
        $data_arr = NoticePlat::getOne($mc, $lang) ?: [];

        $data['data_list'] = $data_arr;//print_r($data['data_list']);exit();
        $data['mc'] = $mc;
        $data['lang'] = $lang;
        
        return Admin::content(function($content) use($data){
            $content->header('mc edit');

            $content->row(Html::append([
                Html::button('+添加+','/admin/notice-plat/create?mc='.$data['mc']."&lang=".$data['lang'],'btn-goto'),
                Html::deleteinput(),
                Html::deleteButton($_SERVER['SERVER_NAME'])
            ]));
            foreach ($data['data_list'] as $one) {
                $data['one'] = $one;
                $grid = Admin::Form(NoticePlat::class, function(Form $form)use($data){
                    $one = $data['one'];
                    //$one->area_list = explode(',', $one->area_list);
                    $form->text('version', 'version')->default($one['version']);
                    $form->textarea('notice', 'notice')->default($one['notice']);
                    $form->text('notice_types', 'notice types')->default($one['notice_types']);
                    //$form->textarea('prop', 'attach')->default($one->prop);

                    $form->disablePjax();
                    $form->setAction('/admin/notice-plat/api-save?mc='.$data['mc'].'&id='.$one['id']."&lang=".$data['lang']);
                    //$form->button('delete')->on('click',"window.location.href = 'http://admin2.com/admin/notice-plat/delete?mc='+$data[mc]+'&id='+$one[id]");
//                    $form->button('delete')->on('click',"alert($('#test').val());");
                });
                $content->row($grid);
            }   
        });
    }

    //添加一条公告
    public function anyAdd(Request $request){
        //获取json数据转数组
        $mc = $request->get('mc');
        $lang = $request->get('lang');
        $data = NoticePlat::getOne($mc, $lang);
        //添加一个空数组
        $nulldata = ['id'=>end($data)['id']+1,'version'=>null,'notice_types'=>null,'notice'=>null];
        array_push($data,$nulldata);

        //写入json文件
        NoticePlat::save_static($data,$mc, $lang);
        //返回上一页面
        //return "<script>history.go(-1);</script>";
    }

    //删除一条公告
    public function delete(Request $request){
        //获取json数据转数组
        $mc = $request->get('mc');
        $id = $request->get('id');
        if ($id == null){
            return "<script>history.go(-1);</script>";
        }
        $lang = $request->get('lang');
        $data = NoticePlat::getOne($mc, $lang);
        if (count($data)>=$id) {
            array_splice($data, $id - 1, 1);
            NoticePlat::save_static($data, $mc, $lang);
            return "<script>history.go(-1);</script>";
        }else{
            return "<script>history.go(-1);</script>";
        }
//        for ($i = 0; $i <= count($data); $i++){
//            if ($data[$i]['id'] == $id){
//                array_splice($data,$i,1);
//                NoticePlat::save_static($data,$mc);
//                return "<script>history.go(-1);</script>";
//            }
//        };
    }

    public function anyApiSave()
    {
        $req = Input::all();

        $url = url()->to("/admin/notice");
        
        
        $data_list = NoticePlat::getOne($req['mc'], $req['lang'])? : [];
        
        $one = &$data_list[$req['id']];
        \Log::debug("plat notice save get_one", ['data'=>$data_list, 'url' =>$req]);
        if ( empty($one)) {
            echo json_encode(['code' => 444, 'msg' => admin_toastr('notice-plat save area or content is empty!','error'), 'data' => $req]);
            return;
        }
        NoticePlat::assignData($one, $req);

        NoticePlat::save_static($data_list, $req['mc'], $req['lang']);
        
        echo json_encode(['code' => 0,'status'=>true, 'msg' => admin_toastr('notice-plat save!','success')]);
        return;     
    }
}
